require(['./config'],() => {
    require(['template','jquery','header','footer'] ,(template,$,header) => {
        class detail {
            constructor () {
                this.getDetail().then(()=>{
                    this.onClick()
                })
            }

            getDetail () {
                return new Promise (resolve => {
                    $.get('http://rap2.taobao.org:38080/app/mock/258485/api/home/lvList',resp => {
                        const id =location.search.slice(4)
                        let{ list } = resp.body
                        list = list.filter(item => {
                            if(item.id == id){
                                return item
                            }
                        })
                        const {
                            price,
                            src,
                            title
                        } = list[0]
                        this.detail = {
                                id,
                                price,
                                src,
                                title
                            }
                            $('#lvshowUl').html(template('lvshowUlTemplate', {...this.detail}))
                            resolve()   
                })
               })
            }
            onClick () {
                //加入购物车
                //使用localstorage来存
                $('.cart').on('click', () =>  {
                    //先取数据
                    let cart = localStorage.getItem('cart')
                    if (cart) {
                        cart = JSON.parse(cart)
                        console.log(cart)
                        //判断购物车有没有当前数据
                        const isExist = cart.some(shop => {
                            return shop.id === this.detail.id
                        })
                        if(isExist) {
                            //当前数据在我购物车已存在 数量加加
                            cart = cart.map( shop => {
                                if (shop.id === this.detail.id) shop.num++
                                //无论是否满足if条件都需要return
                                return shop
                            })
                        } else {
                            cart.push({...this.detail , num : 1 ,check: true})
                        }
                        localStorage.setItem('cart',JSON.stringify(cart))   
                     } else {
                        //购物车为空
                        //存一个数组 ，这个数组里只有this.detail 这一条数据
                        //存详情数量的时候 可以默认为1
                        //默认check也就是选中状态为true
                        
                        localStorage.setItem('cart',JSON.stringify([{...this.detail,num:1,check: true}]))
                        
                    }
                    header.calcCartCount ()
                })
            }
            render () {
                //从地址栏取到id 根据id像后端发送请求获取数据 
               const id =  location.search.slice(4)
            }
        }
        return new detail
    })
})